<template>
  <div class="login-main">
    <div class="login-form">
      <h2>喵喵喵后台管理注册</h2>
      <el-form label-width="80px" class="form" :model="searchForm" :rules="searchRules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="searchForm.username" size="large"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="searchForm.password" type="password" size="large"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="searchForm.name" size="large"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="contactNumber">
          <el-input v-model="searchForm.contactNumber" size="large"></el-input>
        </el-form-item>
        <el-form-item label="学号" prop="studentId">
          <el-input v-model="searchForm.studentId" size="large"></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button @click="userSign(searchForm)">注册</el-button>
          <el-button @click="userLogin">返回</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
// import useStore from '../../stores/user'
import router from '@/router'
import { ElMessage } from 'element-plus'
import { reactive, toRefs } from 'vue'
import { registrationApi } from '../../api/users/login'
// const store = useStore()

const state = reactive({
  searchForm: {}
})
const userSign = async (searchForm) => {
  try {
    const res = await registrationApi(searchForm)
    if (res.success) {
      ElMessage.success('注册成功')
      router.push('/login')
    } else {
      ElMessage.error(res.msg)
    }
  } catch (error) {
    console.log(error)
  }
}
const searchRules = reactive({
  name: [
    { required: true, message: '姓名不能为空', trigger: 'blur' },
    { min: 2, max: 20, message: '姓名长度2-30位', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 6, max: 20, message: '用户名长度6-30位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度6-30位', trigger: 'blur' }
  ],
  contactNumber: [
    { required: true, message: '手机号不能为空', trigger: 'blur' },
    { min: 11, max: 11, message: '手机号长度11位', trigger: 'blur' }
  ],
  studentId: [
    { required: true, message: '学号不能为空', trigger: 'blur' },
    { min: 10, max: 10, message: '学号长度10位', trigger: 'blur' }
  ]
})
const userLogin = () => {
  router.push('/login')
}
const { searchForm } = toRefs(state)
</script>
<style lang="scss" scoped>
.login-main {
  position: relative;
  height: 100%;
  width: 100%;
  background: url(../../assets/img/img1.jpg);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .login-form {
    position: relative;
    left: 50%;
    top: 20%;
    transform: translate(-50%);
    width: 400px;
    height: 430px;
    background: rgba(224, 170, 212, 0.6);
    border-radius: 10px;
    padding: 16px;
    box-sizing: border-box;
    h2 {
      text-align: center;
    }
    .form {
      position: relative;
      top: 30px;
    }
    .login-btn {
      width: 100%;
      text-align: center;
    }
  }
}
</style>
